package org.devocean.groupbuying.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@SuppressWarnings("serial")
@Entity
public class Product implements Serializable {

	@Id
	@GeneratedValue
	private Integer productID;

	private Boolean availability;

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "articlesLocalizationID", referencedColumnName = "articlesLocalizationID", nullable = false)
	@Fetch(FetchMode.JOIN)
	private ArticlesLocalization articlesLocalization;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "merchantID", referencedColumnName = "merchantID", nullable = false)
	private Merchant merchant;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "product")
	private Set<Deal> deals = new HashSet<Deal>();

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
	private Set<Plan> plans = new HashSet<Plan>();

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
	private Set<Rating> ratings = new HashSet<Rating>();

	private Integer rating;

	public Integer getProductID() {
		return productID;
	}

	public void setProductID(Integer productID) {
		this.productID = productID;
	}

	public boolean getAvailability() {
		return availability;
	}

	public void setAvailability(Boolean availability) {
		this.availability = availability;
	}

	public ArticlesLocalization getArticlesLocalization() {
		return articlesLocalization;
	}

	public void setArticlesLocalization(
			ArticlesLocalization articlesLocalization) {
		this.articlesLocalization = articlesLocalization;
	}

	public Merchant getMerchant() {
		return merchant;
	}

	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	public Set<Plan> getPlans() {
		return plans;
	}

	public void setPlans(Set<Plan> plans) {
		this.plans = plans;
	}

	public Set<Deal> getDeals() {
		return deals;
	}

	public void setDeals(Set<Deal> deals) {
		this.deals = deals;
	}

	public Integer getRating() {
		return rating;
	}

	public void setRating(Integer rating) {
		this.rating = rating;
	}

	public Set<Rating> getRatings() {
		return ratings;
	}

	public void setRatings(Set<Rating> ratings) {
		this.ratings = ratings;
	}

	@PrePersist
	@PreUpdate
	@PostLoad
	public void calculRating() {
		Integer sum = 0;
		for (Rating r : this.ratings) {
			sum = sum + r.getPoints();
		}
		this.rating = sum;
	}

}
